package com.carkeeper.user.controller;


import com.alibaba.excel.EasyExcel;
import com.carkeeper.common.utils.PageResult;
import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.user.pojo.User;
import com.carkeeper.user.pojo.UserAndCar;
import com.carkeeper.user.service.UserService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "车主接口api")//标签专门用来标注类的
public class UserController {
    @Autowired
    private UserService userService;

    /*
     * 条件查询+分页查询
     * */
    @GetMapping("/findPage/{page}/{size}")
    @ApiOperation("分页查询+条件查询接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "nickname",value = "车主名称/昵称"),
            @ApiImplicitParam(name = "phone",value = "手机号"),
            @ApiImplicitParam(name = "status",value = "状态"),
            @ApiImplicitParam(name = "page",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页展示条数条数"),

    })
    public Result findPage(User user, @PathVariable(value = "page",required = false) Integer page, @PathVariable(value = "size", required = false) Integer size){
        if (page ==null){
            page = 1;
        }
        if (size == null){
            size = 15;
        }
        try {
            Page<User> page1 = userService.findPage(user, page, size);
            PageResult pageResult = new PageResult(page1.getTotal(),page1.getResult());
            return new Result(true, StatusCode.OK,"查询成功",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }


    /*
    * 锁定/解锁车主主体
    * */
    @PutMapping("/lock/{id}")
    @ApiOperation("锁定/解锁接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "id",value = "主键")
    })
    public Result lock(@PathVariable("id") Long id){
        try {
            userService.lock(id);
            return new Result(true,StatusCode.OK,"锁定成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"锁定失败");
        }
    }

    /*
    * 文件下载
    * */
    @RequestMapping("/download")
    @ApiOperation("POI报表导出接口")//标注方法
    public void download(HttpServletResponse response) throws Exception{
        List<UserAndCar> list = userService.findAll();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName= URLEncoder.encode("车主和车主的车","UTF-8");
        response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
        EasyExcel.write(response.getOutputStream(), UserAndCar.class).sheet().doWrite(list);
    }

    /*
     * 根据车主id查询车主信息
     * */
    @GetMapping("/findById/{id}")
    @ApiOperation("id查询")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "id",value = "主键")
    })
    public Result<User> findById(@PathVariable("id") Long id){
        try {
            User user = userService.findById(id);
            return new Result<User>(true,StatusCode.OK,"查询成功",user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result<User>(false,StatusCode.ERROR,"查询失败");
        }
    }
}
